package stripesbook.view;

import java.util.Collection;
import net.sourceforge.stripes.action.ActionBean;
import org.stripesbook.rayures.view.ViewTag;
import org.stripesbook.rayures.view.ViewTemplate;
import org.stripesbook.rayures.view.stripes.SLink;
import org.stripesbook.rayures.view.stripes.SParam;
import stripesbook.action.user.ContactFormActionBean;
import stripesbook.action.user.ContactListActionBean;
import stripesbook.model.Contact;

public class ContactTable extends ViewTemplate<ContactTable> {
  private Collection<Contact> contacts;

  public ContactTable init(Collection<Contact> contacts) {
    this.contacts = contacts;
    return getThis();
  }

  public Collection<Contact> getContacts() {
    return contacts;
  }

  public Object[] getLinksFor(Contact contact) {
    return new Object[] {
      getLinkFor(contact, ContactListActionBean.class, "details", "#contact_details", "info.gif"),
      getLinkFor(contact, ContactFormActionBean.class, "form", "#contact_form", "update.png"),
      getLinkFor(contact, ContactListActionBean.class, "delete", "#contact_table", "delete.gif"),
    };
  }

  private Object getLinkFor(Contact contact, Class<? extends ActionBean> beanclass, String event,
    String section, String image)
  {
    String contextPath = getViewContext().getRequest().getContextPath();

    return tag(SLink.class).beanclass(beanclass).event(event)
      .onclick("return ajaxLink(this, '" + section + "');")
      .add(tag(SParam.class).name("contact").value(contact.getId()))
      .add(tag(ViewTag.class).tagName("img").attr("src", contextPath + "/images/" + image)
        .attr("border", "0"));
  }
}
